<?php
class SubirController extends Zend_Controller_Action{ 
    public function init()
    {
        $formLogin = new Application_Form_Login();
        $this->view->formLogin = $formLogin;
        $model =new Application_Model_Noticias();
        $this-> view->noticias=$model->getAll();
        $modelTesis =new Application_Model_Tesis();
        $this-> view->tesis=$modelTesis->getAll();
        $modelPersona =new Application_Model_Personas();
        $this-> view->persona=$modelPersona->getAll();
        if(Zend_Auth::getInstance()->hasIdentity()){
            $skin=Zend_Auth::getInstance()->getIdentity()->skin;
            $this->_helper->Cambiarskin($skin);
        }
    }
    public function subirresumenAction(){
   
        $formResumen=new Application_Form_Resumen();
        $modelResumen=new Application_Model_Resumenes();
        if( $this->getRequest()->isPost() ){
            if( $formResumen->isValid( $this->_getAllParams() )) {        
                $modelResumen->save($formResumen->getValues());
 
                $resumen=$modelResumen->getAll();
                $count=count($resumen);
                $id_resumen=$resumen[$count-1]->id_resumen;
                $archivo= $formResumen->archivo->getTransferAdapter();
                $archivo->addFilter('Rename', array('target' => APPLICATION_PATH . 
                                    '/forms/resumenes/resumen_'. $id_resumen .' .pdf','overwrite' => true));
                var_dump( $archivo->receive() );
                return $this->_redirect('/subir/subirvideo');
            }
        }
        $this->view->formResumen=$formResumen;
    }
    public function subirvideoAction(){

        $formVideoscortos=new Application_Form_VideoCorto();
        $modelVideoscorto=new Application_Model_VideoCortos();
        if( $this->getRequest()->isPost() ){

            if( $formVideoscortos->isValid( $this->_getAllParams() )) {  
                $video = $formVideoscortos->getValue('archivo');
                exec('avconv -i '.APPLICATION_PATH .'/forms/videos/'.$video.' 2>&1', $output);
                exec('du -h '.APPLICATION_PATH .'/forms/videos/'.$video.' 2>&1', $output1);
                
                
                //////////////////////duracion//////////////////////////////////////////////                         
                
                $separado_por_comas = implode(",", $output);
                $duracionarray = explode(",",$separado_por_comas);
                
                
                ///////////////////////////////////////////////////////////////////////////////
                 foreach ($duracionarray as $value) {
                   //$dur = substr($value,'11');
                   $bus = "Duration";
                   $verifi = strpos($value, $bus);
                   if($verifi != FALSE) {
                        $duraciontodo = substr($value,'11');
                        $duracion = substr($duraciontodo,'0','9');
                        break;
                   }
                  
                }
                
                //////////////////////end duracion//////////////////////////////////////////////
                //
                //////////////////////calidad//////////////////////////////////////////////                         
                
                $sep_por_comas = implode(",", $output);
                $calidadarray = explode(",",$sep_por_comas);
                
                
                ///////////////////////////////////////////////////////////////////////////////
                 foreach ($calidadarray as $value) {
                   //$dur = substr($value,'11');
                   $bus = "bitrate";
                   $verifi = strpos($value, $bus);
                   if($verifi != FALSE) {
                        $calidadtodo = substr($value,'11');
                        $calidad = substr($calidadtodo,'0','9');
                        break;
                   }
                  
                }
                
                //////////////////////end calidad//////////////////////////////////////////////
                //
                /////////////////////////////tamño//////////////////////////////////////////////
                
                $tamanioocadena = $output1['0'];
                $tamanioarray = explode("/", $tamanioocadena);
                $tamanio =$tamanioarray['0'];
                
                /////////////////////////////end tamño//////////////////////////////////////////////
                
                /////////////////////////////formato//////////////////////////////////////////////
                $formatocadena = $output1['0'];
                $formatoarray = explode("/", $formatocadena);
                $formatocnvideo =$formatoarray['8'];
                $formato= substr($formatocnvideo,'-3');
                
                /////////////////////////////end formato//////////////////////////////////////////////
                ///////////////////////////////Rename movi //////////////////////////////////////////
                $videoC=$modelVideoscorto->getAll();
                $count=count($videoC);
                $id_vid=$videoC[$count-1]->id_video_corto;
                $id_video = $id_vid+1;
                exec('mv '.APPLICATION_PATH .'/forms/videos/'.$video.' '.APPLICATION_PATH .'/forms/videos/video_corto_'.$id_video , $nombre);
                $name = 'vieo_corto_'.$id_video;
                ///////////////////////////////end Rename movi //////////////////////////////////////////

                $modelVideoscorto->save($formVideoscortos->getValues(),$duracion,$formato,$tamanio,$name , $calidad);
                $writer = new Zend_Log_Writer_Firebug();
                $logger = new Zend_Log($writer);

                $logger->log($duracion, Zend_Log::INFO);
                echo '<script type="text/javascript">';
                echo 'Mostrar()';
                echo '</script>';
            }
        }
        $this->view->formVideocorto=$formVideoscortos;
    }
    
}

